package com.example.demo.controller;

import com.example.demo.log.Log;
import com.example.demo.log.LogModuleEnum;
import com.example.demo.model.core.Add;
import com.example.demo.model.core.PageVo;
import com.example.demo.model.core.R;
import com.example.demo.model.core.Update;
import com.example.demo.model.form.DeptForm;
import com.example.demo.model.query.DeptQuery;
import com.example.demo.model.vo.DeptVO;
import com.example.demo.model.vo.OptionVO;
import com.example.demo.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/dept")
@Tag(name = "部门接口")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPerm('sys:dept:add')")
    @Operation(summary = "新增部门")
    @Log(value = "新增部门", module = LogModuleEnum.DEPT)
    public R<Void> add(@Validated(Add.class) @RequestBody DeptForm form) {
        sysDeptService.add(form);
        return R.ok();
    }

    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('sys:dept:edit')")
    @Operation(summary = "修改部门")
    @Log(value = "修改部门", module = LogModuleEnum.DEPT)
    public R<Void> update(@Validated(Update.class) @RequestBody DeptForm form) {
        sysDeptService.update(form);
        return R.ok();
    }

    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPerm('sys:dept:delete')")
    @Operation(summary = "删除部门")
    @Log(value = "删除部门", module = LogModuleEnum.DEPT)
    public R<Void> delete(@RequestBody List<String> ids) {
        sysDeptService.delete(ids);
        return R.ok();
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('sys:dept:query')")
    @Operation(summary = "部门列表")
    public R<PageVo<DeptVO>> page(@Validated DeptQuery query) {
        PageVo<DeptVO> page = sysDeptService.page(query);
        return R.ok(page);
    }

    @GetMapping("/form")
    @Operation(summary = "部门表单")
    public R<DeptForm> form(@Parameter(description = "部门ID") String id) {
        DeptForm form = sysDeptService.form(id);
        return R.ok(form);
    }

    @GetMapping("/options")
    @Operation(summary = "部门树")
    public R<List<OptionVO<String>>> options(@Validated DeptQuery query) {
        List<OptionVO<String>> list = sysDeptService.options(query);
        return R.ok(list);
    }
}
